package workqueue;

import com.rabbitmq.client.*;
import utils.RabbitMQUtils;

import java.io.IOException;

/**
 * @author by Jak
 * @date 2020/7/7
 */
public class Consumer1 {
    public static void main(String[] args) throws IOException {
        //获取连接
        Connection connection = RabbitMQUtils.getConnection();
        final Channel channel = connection.createChannel();
        channel.basicQos(1);//每一次只能消费一个消息
        channel.queueDeclare("work", true, false, false, null);
        //参数1：队列名称 参数2：消息自动确认 true 消费者自动向rabbitmq确认消息消费 false不会自动确认消息
        channel.basicConsume("work", false, new DefaultConsumer(channel) {
            @Override //最后一个参数：消息队列中取出的消息
            public void handleDelivery (String consumerTag, Envelope envelope, AMQP.BasicProperties properties,byte[] body) throws IOException {
                System.out.println("消费者-1: " + new String(body));
                try {
                    Thread.sleep(2000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //参数1：确认队列中那个具体消息  参数2：是否开启多个消息同时确认
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });
    }
}
